var page = {
    'totalCount' : '共{count}条',
    'pre' : '上一页',
    'next' : '下一页',
    'pageTo' : '',
    'page' : '',
    'go' : '跳转',
};
(function($) {
    var ms = {
        init : function(obj, args) {
            ms.fillHtml(obj, args);
            ms.bindEvent(obj, args);
        },
        // 填充html
        fillHtml : function(obj, args) {
            return (function() {
                obj.empty();
                if (args.pageCount < 2) {
                    return;
                }
                if (args.totalCount && args.totalCount > 0 && page.totalCount) {
                    obj.append('<span class="pageInfo">' + page.totalCount.replace('{count}', args.totalCount)
                            + '</span>');
                }
                // 上一页
                if (args.current > 1) {
                    obj.append('<span class="btn btn-default prevPage">' + page.pre + '</span>');
                } else {
                    obj.remove('.prevPage');
                    obj.append('<span class="btn btn-default disabled">' + page.pre + '</span>');
                }
                // 中间页码
                if (args.current != 1 && args.current >= 4 && args.pageCount != 4) {
                    obj.append('<span class="btn btn-default tcdNumber">' + 1 + '</span>');
                }
                if (args.current - 2 > 2 && args.current <= args.pageCount && args.pageCount > 5) {
                    obj.append('<span>...</span>');
                }
                var start = args.current - 2, end = args.current + 2;
                if ((start > 1 && args.current < 4) || args.current == 1) {
                    end++;
                }
                if (args.current > args.pageCount - 4 && args.current >= args.pageCount) {
                    start--;
                }
                for (; start <= end; start++) {
                    if (start <= args.pageCount && start >= 1) {
                        if (start != args.current) {
                            obj.append('<span class="btn btn-default tcdNumber">' + start + '</span>');
                        } else {
                            obj.append('<span class="btn btn-default current">' + start + '</span>');
                        }
                    }
                }
                if (args.current + 2 < args.pageCount - 1 && args.current >= 1 && args.pageCount > 5) {
                    obj.append('<span>...</span>');
                }
                if (args.current != args.pageCount && args.current < args.pageCount - 2 && args.pageCount != 4) {
                    obj.append('<span class="btn btn-default tcdNumber">' + args.pageCount + '</span>');
                }
                // 下一页
                if (args.current < args.pageCount) {
                    obj.append('<span class="btn btn-default nextPage">' + page.next + '</span>');
                } else {
                    obj.remove('.nextPage');
                    obj.append('<span class="btn btn-default disabled">' + page.next + '</span>');
                }
                if (args.pageCount > 7) {
                    var html = '<div class="go-addon">';
                    html += '<div class="input-group goPage">' + page.pageTo;
                    html += '<input type="text" class="form-control toPageNumber" onkeyup="';
                    html += 'this.value=this.value.replace(/\\D/ig,\'\'),this.value=this.value>' + args.pageCount + '?'
                            + args.pageCount + ':this.value,this.value=(this.value|0)<1?1:this.value';
                    html += '" value="' + args.current + '"/>';
                    html += page.page
                            + '<span class="input-group-btn"><button class="btn btn-primary gotoPage" type="button">'
                            + page.go + '</button></span></div></div>';
                    obj.append(html);
                }
            })();
        },
        // 绑定事件
        bindEvent : function(obj, args) {
            return (function() {
                obj.unbind('click');
                obj.on("click", "span.tcdNumber", function() {
                    var current = parseInt($(this).text());
                    ms.fillHtml(obj, {
                        "current" : current,
                        "pageCount" : args.pageCount,
                        "totalCount" : args.totalCount
                    });
                    if (typeof (args.backFn) == "function") {
                        args.backFn(current);
                    }
                });
                // 上一页
                obj.on("click", "span.prevPage", function() {
                    var current = parseInt(obj.children("span.current").text());
                    ms.fillHtml(obj, {
                        "current" : current - 1,
                        "pageCount" : args.pageCount,
                        "totalCount" : args.totalCount
                    });
                    if (typeof (args.backFn) == "function") {
                        args.backFn(current - 1);
                    }
                });
                // 下一页
                obj.on("click", "span.nextPage", function() {
                    var current = parseInt(obj.children("span.current").text());
                    ms.fillHtml(obj, {
                        "current" : current + 1,
                        "pageCount" : args.pageCount,
                        "totalCount" : args.totalCount
                    });
                    if (typeof (args.backFn) == "function") {
                        args.backFn(current + 1);
                    }
                });
                // 跳转到
                obj.on("click", '.gotoPage', function() {
                    var current = parseInt(obj.find(".goPage .toPageNumber").val());
                    ms.fillHtml(obj, {
                        "current" : current,
                        "pageCount" : args.pageCount,
                        "totalCount" : args.totalCount
                    });
                    if (typeof (args.backFn) == "function") {
                        args.backFn(current);
                    }
                });
            })();
        }
    }
    $.fn.createPage = function(options) {
        var args = $.extend({
            pageCount : 10,
            current : 1,
            totalCount : 0,
            backFn : function() {
            }
        }, options);
        ms.init(this, args);
    }
})(jQuery);
